﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lavka.Core;
using Lavka.Models;

namespace Lavka.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class ImportController : Controller
    {
        public int ListPageIndex
        {
            get
            {
                if (Session["PriceProductListPageIndex"] == null) Session["PriceProductListPageIndex"] = 0;
                return (int)Session["PriceProductListPageIndex"];
            }
            set { Session["PriceProductListPageIndex"] = value; }

        }

        public static List<PriceProduct> PriceProducts { get; set; }
        private LavkaDbEntities context = new LavkaDbEntities();

        public ActionResult Index()
        {
            ListPageIndex = 0;
            return View();
        }
        [HttpPost]
        public ActionResult ResetStorage()
        {
            foreach (var product in context.Products)
            {
                product.Available = false;
                context.ObjectStateManager.ChangeObjectState(product, EntityState.Modified);
            }
            context.SaveChanges();
            ViewBag.IsReseted = true;
            return View("Index", new { ListPageIndex = 0});
        }
        [HttpPost]
        public ActionResult Index(HttpPostedFileBase file)
        {
            string priceList = FileHelper.UploadPrice(file);
            PriceProducts = ImportHelper.GetProductsFromPrice(priceList);
            var priceProducts = PriceProducts.OrderBy(p => p.Name).Skip(15 * ListPageIndex).Take(15).ToList();
            return View("Details", priceProducts);
        }

        [HttpPost]
        public PartialViewResult LoadPriceProductPart()
        {
            ListPageIndex++;
            ViewData["Brands"] = new SelectList(context.Brands.ToList(), "BrandId", "Name");
            ViewData["Categories"] = new SelectList(CategoriesHelper.GetCategories(false, context), "CategoryId", "Name");
            var priceProducts = PriceProducts.OrderBy(p => p.Name).Skip(15 * ListPageIndex).Take(15).ToList();
            return PartialView("PriceProductList", priceProducts);
        }

        [HttpPost]
        public ActionResult SendProductToBase(PriceProduct priceProduct)
        {
            Products product = null;
            if (!priceProduct.IsInBase)
            {
                product = new Products
                {
                    BrandId = Convert.ToInt32(Request.Form["ddlBrands"]),
                    CategoryId = Convert.ToInt32(Request.Form["ddlCategories"]),
                    Name = priceProduct.Name,
                    Available = priceProduct.Availability,
                    Price = priceProduct.Price,
                    Reference = priceProduct.Reference,
                    Description = priceProduct.Description
                };
                context.Products.AddObject(product);
                context.SaveChanges();
                priceProduct.IsInBase = true;
                ViewBag.UpdateMessage = "Добавлен";
            }
            else
            {
                if (ModelState.IsValid)
                {
                    product = ProductsHelper.GetProductByReference(priceProduct.Reference, context);
                    if (product != null)
                    {
                        product.BrandId = Convert.ToInt32(Request.Form["ddlBrands"]);
                        product.CategoryId = Convert.ToInt32(Request.Form["ddlCategories"]);
                        product.Name = priceProduct.Name;
                        product.Available = priceProduct.Availability;
                        product.Price = priceProduct.Price;
                        product.Reference = priceProduct.Reference;
                        product.Description = priceProduct.Description;

                        context.ObjectStateManager.ChangeObjectState(product, EntityState.Modified);
                        ViewBag.UpdateMessage = "Обновлен";
                        context.SaveChanges();
                    }
                }                
            }
            ViewData["Brands"] = new SelectList(context.Brands.ToList(), "BrandId", "Name", product.BrandId);
            ViewData["Categories"] = new SelectList(CategoriesHelper.GetCategories(false, context), "CategoryId", "Name", product.BrandId);

            return PartialView("PriceProduct", priceProduct);
        }

        public ActionResult Details()
        {
            if (ValidateRequest)
            {
                var priceProducts = PriceProducts.OrderBy(p => p.Name).Skip(15 * ListPageIndex).Take(15).ToList();
                return View("Details", priceProducts);
            }
            else
            {
                throw new Exception("Товары не найдены!");
            }
        }

    }
}
